import Vue from 'vue'
import tool from '~/plugins/tool'

import query from '~/mixins/query';
Vue.prototype.tool = tool

// window.location.href
// 'https://admin.fortunajewellery.net/'
Vue.prototype.img = 'https://admin.fortunajewellery.net/'

Vue.mixin(query)





// const queryString = window.location.search;

// let params = {}
// if (queryString) {
//     // 去除开头的问号字符 "?"
//     const paramsString = queryString.substring(1);

//     // 将查询参数字符串拆分为键值对数组
//     const paramsArray = paramsString.split('&');

//     // 创建一个对象来存储参数
//     // const params = {};

//     // 遍历键值对数组，将参数存储到对象中
//     paramsArray.forEach(param => {
//         const [key, value] = param.split('=');
//         params[key] = decodeURIComponent(value);
//     });
//     params = params
// }


// console.log(params);
// function getCookieValue(cookieName) {
//     const cookies = document.cookie.split(';');
//     console.log('cookies==> ', cookies);
//     for (let i = 0; i < cookies.length; i++) {
//         const cookie = cookies[i].trim();
//         if (cookie.startsWith(cookieName + '=')) {
//             return cookie.substring(cookieName.length + 1);
//         }
//     }
//     return null;
// }





// const myCookieValue = getCookieValue('lg');
// console.log('myCookieValue==> ', myCookieValue);
// const queryLg = params.lang;
// console.log('queryLg==> ', queryLg);

// if (queryLg == 'cn') {
//     Vue.prototype.lg = 1
//     console.log('9696==> ', 9696);

// }


// if (queryLg == 'en') {
//     Vue.prototype.lg = 0
//     console.log('8585==> ', 8585);

// }



// if (queryLg !== 'en' && queryLg !== 'cn') {

//     if (myCookieValue) {
//         if (myCookieValue == 'zh-CN') {
//             Vue.prototype.lg = 1
//         } else {
//             Vue.prototype.lg = 0
//         }
//     } else {
//         if (navigator.language == 'zh-CN') {
//             Vue.prototype.lg = 1
//         } else {
//             Vue.prototype.lg = 0
//         }
//     }

// }

const queryString = window.location.search;
const params = {};

if (queryString) {
    const paramsString = queryString.substring(1);
    const paramsArray = paramsString.split('&');

    paramsArray.forEach(param => {
        const [key, value] = param.split('=');
        params[key] = decodeURIComponent(value);
    });
}

function getCookieValue(cookieName) {
    const cookies = document.cookie.split(';');
    for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim();
        if (cookie.startsWith(cookieName + '=')) {
            return cookie.substring(cookieName.length + 1);
        }
    }
    return null;
}

// 获取 cookie 中的 lg 值
const myCookieValue = getCookieValue('lg');
// 从 URL 参数中获取 lg 值
const queryLg = params.lang;

let lg;
// 根据 lg 值设置 Vue 的全局变量
if (queryLg === 'cn') {
    lg = 1; // 简体中文
} else if (queryLg === 'en') {
    lg = 0; // 英语
} else if (queryLg === 'zh') {
    lg = 2; // 繁体中文
} else {
    // 如果 URL 参数中没有 lg 值，则从 cookie 或浏览器语言中获取
    if (myCookieValue) {
        if (myCookieValue === 'zh-CN') {
            lg = 1; // 简体中文
        } else if (myCookieValue === 'en') {
            lg = 0; // 英语
        } else if (myCookieValue === 'zh') {
            lg = 2; // 繁体中文
        }
    } else {
        let language = navigator.language;
        console.log('language==> ', language);
        if (language === 'zh-CN') {
            lg = 1; // 简体中文
        } else if (language === 'en') {
            lg = 0; // 英语
        } else if (language === 'zh-TW') {
            lg = 2; // 繁体中文
        }
    }
}

// 将 lg 值绑定到 Vue 的全局变量上
Vue.prototype.lg = lg;












Vue.filter('formatTime1', function (timestamp) {
    const date = new Date(timestamp * 1000); // 将秒数转换为毫秒数
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${year}`;
});
Vue.filter('formatTime2', function (timestamp) {
    const date = new Date(timestamp * 1000); // 将秒数转换为毫秒数
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${month}-${day} `;
});


Vue.filter('formatTime', function (timestamp) {
    const date = new Date(timestamp * 1000); // 将秒数转换为毫秒数
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${year}-${month}-${day} `;
});
